package com.crayfish.quduo.presenter;

import com.crayfish.quduo.base.RxPresenter;
import com.crayfish.quduo.model.UserBean;
import com.crayfish.quduo.presenter.contract.PersonalContract;
import com.crayfish.quduo.utils.LogUtils;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by crayfish on 2017/3/17.
 */
public class PersonalPresenter extends RxPresenter<PersonalContract.View> implements PersonalContract.Presenter {

    @Override
    public void getUser() {
        UserBean userBean = BmobUser.getCurrentUser(UserBean.class);
        if(userBean != null){
            mView.showUser(userBean);
        }
    }

    @Override
    public void updateUsername(final String value) {
        UserBean newUser = new UserBean();
        newUser.setUsername(value);
        final UserBean userBean = BmobUser.getCurrentUser(UserBean.class);
        newUser.update(userBean.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e == null){
                    userBean.setUsername(value);
                    mView.showUser(userBean);
                }else{
                    mView.showError(e.getMessage());
                    LogUtils.d(e.getMessage());
                }
            }
        });
    }

    @Override
    public void updateEmail(final String value) {
        UserBean newUser = new UserBean();
        newUser.setEmail(value);
        final UserBean userBean = BmobUser.getCurrentUser(UserBean.class);
        newUser.update(userBean.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e == null){
                    userBean.setEmail(value);
                    mView.showUser(userBean);
                }else{
                    mView.showError(e.getMessage());
                    LogUtils.d(e.getMessage());
                }
            }
        });
    }

    @Override
    public void updatePhone(final String value) {
        UserBean newUser = new UserBean();
        newUser.setMobilePhoneNumber(value);
        final UserBean userBean = BmobUser.getCurrentUser(UserBean.class);
        newUser.update(userBean.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e == null){
                    userBean.setMobilePhoneNumber(value);
                    mView.showUser(userBean);
                }else{
                    mView.showError(e.getMessage());
                    LogUtils.d(e.getMessage());
                }
            }
        });
    }

    @Override
    public void updateGender(final String value) {
        UserBean newUser = new UserBean();
        newUser.setGender(value);
        final UserBean userBean = BmobUser.getCurrentUser(UserBean.class);
        newUser.update(userBean.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e == null){
                    userBean.setGender(value);
                    mView.showUser(userBean);
                }else{
                    mView.showError(e.getMessage());
                    LogUtils.d(e.getMessage());
                }
            }
        });
    }

    @Override
    public void updateIntroduction(final String value) {
        UserBean newUser = new UserBean();
        newUser.setIntroduction(value);
        final UserBean userBean = BmobUser.getCurrentUser(UserBean.class);
        newUser.update(userBean.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e == null){
                    userBean.setIntroduction(value);
                    mView.showUser(userBean);
                }else{
                    mView.showError(e.getMessage());
                    LogUtils.d(e.getMessage());
                }
            }
        });
    }
}
